/*
 * @Description: 
 * @Author: chaixinyu
 * @Date: 2022-12-24 15:47:13
 * @LastEditors: egg1bro
 * @LastEditTime: 2023-02-08 08:57:46
 */
import { ref, reactive, computed } from 'vue'
import { defineStore } from 'pinia'
import router from '@/router/index'
import { reqUserLogin, reqGetUserInfo, reqGetSms, reqUserLoginBySms } from '@/api/index'
export const useLoginStore = defineStore('login', () => {

    let userInfo = ref({})

    // 用户登录信息
    const getUserLoginInfo = async (params = {}) => {

        let result = await reqUserLogin(params)
        if (result.code == 200) {
            console.log(result.data, 'token');
            // 把获取到的token存储到localStoreage中
            localStorage.setItem('token', result.data)
        }
    }

    // 用户登录(手机验证码)
    const getUserLoginBySms = async (params) => {
        let result = await reqUserLoginBySms(params)
        if (result.code == 200) {
            console.log(result.data, 'token');
            // 把获取到的token存储到localStoreage中
            localStorage.setItem('token', result.data)
            router.push('/me')
        }
    }

    // 获取验证码
    const getSms = async (phone) => {
        await reqGetSms(phone)
    }

    // 获取用户信息
    const getUserInfo = async () => {
        let result = await reqGetUserInfo()

        if (result.status == 200) {
            userInfo.value = result.data
        }
        console.log(userInfo.value, 'userInfo.value');
    }

    return { getUserLoginInfo, getUserInfo, userInfo, getSms, getUserLoginBySms }
})